package 剑指offer_leetcode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * JZ34二叉树中和为某一值的路径2
 */
public class 剑指Offer34_二叉树中和为某一值的路径 {
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> pathSum(TreeNode root, int target) {
        if(root==null)
            return res;
        dfs(root,target);
        return res;
    }

    void dfs(TreeNode root, int target){
        if(root==null)
            return;
        target-=root.val;
        path.add(root.val);
        if(root.left==null && root.right==null && target==0)
            res.add(new ArrayList(path));
        dfs(root.left,target);
        dfs(root.right,target);
        path.removeLast();
        return;
    }
}
